import java.io.BufferedWriter;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;


public class Automated_run {
	
	final static int MAX_PAGES = 20;
	final static int NUM_RUNS = 20;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
//		copy_over_ST(0);
//		run_single_thread();
		run_multi_thread(16, 12);
		
	}

	private static void run_multi_thread(int rtrvr, int prsr) {
		ArrayList<String> keys = new ArrayList<String>();
		LinkedBlockingQueue<String> urlQueue = new LinkedBlockingQueue<String>();
		LinkedBlockingQueue<WebPage> webPageQueue = new LinkedBlockingQueue<WebPage>();
		ConcurrentSkipListSet<String> pagesVisited = new ConcurrentSkipListSet<String>();
		ConcurrentSkipListSet<String> disallowList = new ConcurrentSkipListSet<String>();
		
		keys.add("artificial");
		keys.add("intelligence");
		keys.add("research");
		
		String seedURL = "http://faculty.washington.edu/gmobus/";
		
		urlQueue.add(seedURL);
		System.out.println(keys.toString());
		System.out.println(urlQueue.toString());

		ActualReporter reporter = new ActualReporter(keys, MAX_PAGES);

		DataGatherer dataG = new DataGatherer(reporter);
	
		ExecutorService retrievers = null;
		ExecutorService parsers = null;
		
		retrievers = Executors.newFixedThreadPool(rtrvr);		
		parsers = Executors.newFixedThreadPool(prsr);
		
		for (;;) {
			retrievers.execute(new pageRetriever(webPageQueue, urlQueue, disallowList));
			parsers.execute(new httpParser(webPageQueue, urlQueue, dataG, keys, pagesVisited, disallowList));
		}

	}
	
	private static void run_single_thread() {
		
		ArrayList<String> keys = new ArrayList<String>();
		LinkedBlockingQueue<String> urlQueue = new LinkedBlockingQueue<String>();
		LinkedBlockingQueue<WebPage> webPageQueue = new LinkedBlockingQueue<WebPage>();
		ConcurrentSkipListSet<String> pagesVisited = new ConcurrentSkipListSet<String>();
		ConcurrentSkipListSet<String> disallowList = new ConcurrentSkipListSet<String>();

		keys.add("artificial");
		keys.add("intelligence");
		keys.add("research");
		
		String seedURL = "http://faculty.washington.edu/gmobus/";
		
		urlQueue.add(seedURL);
		System.out.println(keys.toString());
		System.out.println(urlQueue.toString());

		ActualReporter reporter = new ActualReporter(keys, MAX_PAGES);

		DataGatherer dataG = new DataGatherer(reporter);
		
		while (dataG.getTotalPages() <= MAX_PAGES) {
			pageRetriever pr = new pageRetriever(webPageQueue, urlQueue, disallowList);
			httpParser parser = new httpParser(webPageQueue, urlQueue, dataG, keys, pagesVisited, disallowList);
			pr.run();
			parser.run();
			
		}
		System.err.println("threads terminated.");
	}
	
	private static void copy_over_ST(int number) {
		String old_file = "reporterData";
		String old_file_ext = "reporterData.txt";
		StringBuilder new_file = new StringBuilder(old_file);
		new_file.append(String.format("-ST-run-%d.txt", number + 1));
		
		File file_to_rename = new File(old_file_ext);
		File created_file = new File(new_file.toString());
		try {

			BufferedWriter output = new BufferedWriter(new FileWriter(created_file, true));
			BufferedReader input = new BufferedReader(new FileReader(file_to_rename));
			String line = input.readLine();
			while(line != null) {
				output.append(line);
				output.append("\n");
				line = input.readLine();					
			}
			output.close();
			input.close();
		} catch(IOException no_file) { System.err.println("Error with file.");}
	}
}
